home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 May / EnigmA AMIGA RUN 18 (1997)(G.R. Edizioni)(IT)[!][issue 1997-05][EAR-CD II].iso / ghost / gs403src_png.lha / gs4.03 / libpng / example.c < prev    next >
C/C++ Source or Header  |  1996-06-05  |  17KB  |  512 lines

  1. /* example.c - an example of using libpng */
  2.  
  3. /* this is an example of how to use libpng to read and write
  4.    png files.  The file libpng.txt is much more verbose then
  5.    this.  If you have not read it, do so first.  This was
  6.    designed to be a starting point of an implementation.
  7.    This is not officially part of libpng, and therefore
  8.    does not require a copyright notice.
  9.  
  10.    This file does not currently compile, because it is missing
  11.    certain parts, like allocating memory to hold an image.
  12.    You will have to supply these parts to get it to compile.
  13.    */
  14.  
  15. #include <png.h>
  16.  
  17. /* check to see if a file is a png file using png_check_sig() */
  18. int check_png(char * file_name)
  19. {
  20.    FILE *fp;
  21.    char buf[8];
  22.    int ret;
  23.  
  24.    fp = fopen(file_name, "rb");
  25.    if (!fp)
  26.       return 0;
  27.    ret = fread(buf, 1, 8, fp);
  28.    fclose(fp);
  29.  
  30.    if (ret != 8)
  31.       return 0;
  32.  
  33.    ret = png_check_sig(buf, 8);
  34.  
  35.    return (ret);
  36. }
  37.  
  38. /* read a png file.  You may want to return an error code if the read
  39.    fails (depending upon the failure). */
  40. void read_png(char *file_name)
  41. {
  42.    FILE *fp;
  43.    png_structp png_ptr;
  44.    png_infop info_ptr;
  45.  
  46.    /* open the file */
  47.    fp = fopen(file_name, "rb");
  48.    if (!fp)
  49.       return;
  50.  
  51.    /* Create and initialize the png_struct with the desired error handler
  52.       functions.  If you want to use the default stderr and longjump method,
  53.       you can supply NULL for the last three parameters.  We also check that
  54.       the header file is compatible with the library version.
  55.     */
  56.    png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
  57.       (void *)user_error_ptr, user_error_fn, user_warning_fn);
  58.  
  59.    if (!png_ptr)
  60.    {
  61.       fclose(fp);
  62.       return;
  63.    }
  64.  
  65.    info_ptr = png_create_info_struct();
  66.    if (!info_ptr)
  67.    {
  68.       fclose(fp);
  69.       png_destroy_read_struct(&png_ptr,  (png_infopp)NULL, (png_infopp)NULL);
  70.       return;
  71.    }
  72.  
  73.    /* set error handling if you are using the setjmp/longjmp method */
  74.    if (setjmp(png_ptr->jmpbuf))
  75.    {
  76.       /* Free all of the memory associated with the png_ptr and info_ptr */
  77.       png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
  78.       fclose(fp);
  79.       /* If we get here, we had a problem reading the file */
  80.       return;
  81.    }
  82.  
  83.    /* set up the input control if you are using standard C streams */
  84.    png_init_io(png_ptr, fp);
  85.  
  86.    /* if you are using replacement read functions, instead of calling
  87.       png_init_io() here you would call */
  88.    png_set_read_fn(png_ptr, (void *)user_io_ptr, user_read_fn);
  89.    /* where user_io_ptr is a structure you want available to the callbacks */
  90.  
  91.    /* read the file information */
  92.    png_read_info(png_ptr, info_ptr);
  93.  
  94.    /* set up the transformations you want.  Note that these are
  95.       all optional.  Only call them if you want them */
  96.  
  97.    /* expand paletted colors into true RGB triplets */
  98.    if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  99.       png_set_expand(png_ptr);
  100.  
  101.    /* expand grayscale images to the full 8 bits */
  102.    if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY && info_ptr->bit_depth < 8)
  103.       png_set_expand(png_ptr);
  104.  
  105.    /* expand paletted or RGB images with transparency to full alpha channels
  106.     * so the data will be available as RGBA quartets */
  107.    if (info_ptr->valid & PNG_INFO_tRNS)
  108.       png_set_expand(png_ptr);
  109.  
  110.    /* Set the background color to draw transparent and alpha
  111.       images over.  It is possible to set the red, green, and blue
  112.       components directly for paletted images. */
  113.  
  114.    png_color_16 my_background;
  115.  
  116.    if (info_ptr->valid & PNG_INFO_bKGD)
  117.       png_set_background(png_ptr, &(info_ptr->background),
  118.                          PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
  119.    else
  120.       png_set_background(png_ptr, &my_background,
  121.                          PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
  122.  
  123.    /* tell libpng to handle the gamma conversion for you */
  124.    if (info_ptr->valid & PNG_INFO_gAMA)
  125.       png_set_gamma(png_ptr, screen_gamma, info_ptr->gamma);
  126.    else
  127.       png_set_gamma(png_ptr, screen_gamma, 0.45);
  128.  
  129.    /* tell libpng to strip 16 bit/color files down to 8 bits/color */
  130.    if (info_ptr->bit_depth == 16)
  131.       png_set_strip_16(png_ptr);
  132.  
  133.    /* dither rgb files down to 8 bit palette & reduce palettes
  134.       to the number of colors available on your screen */
  135.    if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
  136.    {
  137.       if (info_ptr->valid & PNG_INFO_PLTE)
  138.          png_set_dither(png_ptr, info_ptr->palette, info_ptr->num_palette,
  139.                         max_screen_colors, info_ptr->histogram);
  140.       else
  141.       {
  142.          png_color std_color_cube[MAX_SCREEN_COLORS] =
  143.             {/* ... colors ... */};
  144.  
  145.          png_set_dither(png_ptr, std_color_cube, MAX_SCREEN_COLORS,
  146.             MAX_SCREEN_COLORS, NULL);
  147.       }
  148.    }
  149.  
  150.    /* invert monocrome files to have 0 as white and 1 as black */
  151.    if (info_ptr->bit_depth == 1 && info_ptr->color_type == PNG_COLOR_GRAY)
  152.       png_set_invert(png_ptr);
  153.  
  154.    /* shift the pixels down to their true bit depth */
  155.    if (info_ptr->valid & PNG_INFO_sBIT &&
  156.       info_ptr->bit_depth > info_ptr->sig_bit)
  157.       png_set_shift(png_ptr, &(info_ptr->sig_bit));
  158.  
  159.    /* pack multiple pixels with bit depths of 1, 2, and 4 into bytes
  160.       (useful only for paletted and grayscale images) */
  161.    if (info_ptr->bit_depth < 8)
  162.       png_set_packing(png_ptr);
  163.  
  164.    /* flip the rgb pixels to bgr */
  165.    if (info_ptr->color_type == PNG_COLOR_TYPE_RGB ||
  166.       info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  167.       png_set_bgr(png_ptr);
  168.  
  169.    /* swap bytes of 16 bit files to least significant bit first */
  170.    if (info_ptr->bit_depth == 16)
  171.       png_set_swap(png_ptr);
  172.  
  173.    /* add a filler byte to RGB files (before or after each RGB triplet) */
  174.    if (info_ptr->bit_depth == 8 && info_ptr->color_type == PNG_COLOR_TYPE_RGB)
  175.       png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
  176.  
  177.    /* turn on interlace handling if you are not using png_read_image() */
  178.    number_passes = png_set_interlace_handling(png_ptr);
  179.  
  180.    /* optional call to gamma correct and add the background to the palette
  181.       and update info structure. */
  182.    png_read_update_info(png_ptr, info_ptr);
  183.  
  184.    /* allocate the memory to hold the image using the fields
  185.       of png_info. */
  186.  
  187.    /* the easiest way to read the image */
  188.    png_bytep row_pointers[height];
  189.  
  190.    for (row = 0; row < height; row++)
  191.    {
  192.      row_pointers[row] = malloc(info_ptr->rowbytes);
  193.    }
  194.  
  195.    png_read_image(png_ptr, row_pointers);
  196.  
  197.    /* the other way to read images - deal with interlacing */
  198.  
  199.    for (pass = 0; pass < number_passes; pass++)
  200.    {
  201.       /* Read the image using the "sparkle" effect. */
  202.       png_read_rows(png_ptr, row_pointers, NULL, number_of_rows);
  203.  
  204.       /* If you are only reading on row at a time, this works */
  205.       for (y = 0; y < height; y++)
  206.       {
  207.          png_bytep row_pointers = row[y];
  208.          png_read_rows(png_ptr, &row_pointers, NULL, 1);
  209.       }
  210.  
  211.       /* to get the rectangle effect, use the third parameter */
  212.       png_read_rows(png_ptr, NULL, row_pointers, number_of_rows);
  213.  
  214.       /* if you want to display the image after every pass, do
  215.          so here */
  216.    }
  217.  
  218.    /* read the rest of the file, getting any additional chunks in info_ptr */
  219.    png_read_end(png_ptr, info_ptr);
  220.  
  221.    /* clean up after the read, and free any memory allocated */
  222.    png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
  223.  
  224.    /* close the file */
  225.    fclose(fp);
  226.  
  227.    /* that's it */
  228.    return;
  229. }
  230.  
  231. /* progressively read a file */
  232.  
  233. int
  234. initialize_png_reader(png_structp *png_ptr, png_infop *info_ptr)
  235. {
  236.    /* Create and initialize the png_struct with the desired error handler
  237.       functions.  If you want to use the default stderr and longjump method,
  238.       you can supply NULL for the last three parameters.  We also check that
  239.       the library version is compatible in case we are using dynamically
  240.       linked libraries.
  241.     */
  242.    *png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
  243.        (void *)user_error_ptr, user_error_fn, user_warning_fn);
  244.  
  245.    if (! *png_ptr)
  246.    {
  247.       *info_ptr = NULL;
  248.       return ERROR;
  249.    }
  250.  
  251.    *info_ptr = png_create_info_struct(png_ptr);
  252.  
  253.    if (! *info_ptr)
  254.    {
  255.       png_destroy_read_struct(png_ptr, info_ptr, (png_infopp)NULL);
  256.       return ERROR;
  257.    }
  258.  
  259.    if (setjmp((*png_ptr)->jmpbuf))
  260.    {
  261.       png_destroy_read_struct(png_ptr, info_ptr, (png_infopp)NULL);
  262.       return ERROR;
  263.    }
  264.  
  265.    /* this one's new.  You will need to provide all three
  266.       function callbacks, even if you aren't using them all.
  267.       These functions shouldn't be dependent on global or
  268.       static variables if you are decoding several images
  269.       simultaneously.  You should store stream specific data
  270.       in a separate struct, given as the second parameter,
  271.       and retrieve the pointer from inside the callbacks using
  272.       the function png_get_progressive_ptr(png_ptr). */
  273.    png_set_progressive_read_fn(*png_ptr, (void *)stream_data,
  274.       info_callback, row_callback, end_callback);
  275.  
  276.    return OK;
  277. }
  278.  
  279. int
  280. process_data(png_structp *png_ptr, png_infop *info_ptr,
  281.    png_bytep buffer, png_uint_32 length)
  282. {
  283.    if (setjmp((*png_ptr)->jmpbuf))
  284.    {
  285.       /* Free the png_ptr and info_ptr memory on error */
  286.       png_destroy_read_struct(png_ptr, info_ptr, (png_infopp)NULL);
  287.       return ERROR;
  288.    }
  289.  
  290.    /* this one's new also.  Simply give it chunks of data as
  291.       they arrive from the data stream (in order, of course).
  292.       On Segmented machines, don't give it any more than 64K.
  293.       The library seems to run fine with sizes of 4K, although
  294.       you can give it much less if necessary (I assume you can
  295.       give it chunks of 1 byte, but I haven't tried with less
  296.       than 256 bytes yet).  When this function returns, you may
  297.       want to display any rows that were generated in the row
  298.       callback, if you aren't already displaying them there. */
  299.    png_process_data(*png_ptr, *info_ptr, buffer, length);
  300.    return OK;
  301. }
  302.  
  303. info_callback(png_structp png_ptr, png_infop info)
  304. {
  305. /* do any setup here, including setting any of the transformations
  306.    mentioned in the Reading PNG files section.  For now, you _must_
  307.    call either png_start_read_image() or png_read_update_info()
  308.    after all the transformations are set (even if you don't set
  309.    any).  You may start getting rows before png_process_data()
  310.    returns, so this is your last chance to prepare for that. */
  311. }
  312.  
  313. row_callback(png_structp png_ptr, png_bytep new_row,
  314.    png_uint_32 row_num, int pass)
  315. {
  316. /* this function is called for every row in the image.  If the
  317.    image is interlacing, and you turned on the interlace handler,
  318.    this function will be called for every row in every pass.
  319.    Some of these rows will not be changed from the previous pass.
  320.    When the row is not changed, the new_row variable will be NULL.
  321.    The rows and passes are called in order, so you don't really
  322.    need the row_num and pass, but I'm supplying them because it
  323.    may make your life easier.
  324.  
  325.    For the non-NULL rows of interlaced images, you must call
  326.    png_progressive_combine_row() passing in the row and the
  327.    old row.  You can call this function for NULL rows (it will
  328.    just return) and for non-interlaced images (it just does the
  329.    memcpy for you) if it will make the code easier.  Thus, you
  330.    can just do this for all cases: */
  331.  
  332.    png_progressive_combine_row(png_ptr, old_row, new_row);
  333.  
  334. /* where old_row is what was displayed for previous rows.  Note
  335.    that the first pass (pass == 0 really) will completely cover
  336.    the old row, so the rows do not have to be initialized.  After
  337.    the first pass (and only for interlaced images), you will have
  338.    to pass the current row, and the function will combine the
  339.    old row and the new row. */
  340. }
  341.  
  342. end_callback(png_structp png_ptr, png_infop info)
  343. {
  344. /* this function is called when the whole image has been read,
  345.    including any chunks after the image (up to and including
  346.    the IEND).  You will usually have the same info chunk as you
  347.    had in the header, although some data may have been added
  348.    to the comments and time fields.
  349.  
  350.    Most people won't do much here, perhaps setting a flag that
  351.    marks the image as finished. */
  352. }
  353.  
  354. /* write a png file */
  355. void write_png(char *file_name, ... other image information ...)
  356. {
  357.    FILE *fp;
  358.    png_structp png_ptr;
  359.    png_infop info_ptr;
  360.  
  361.    /* open the file */
  362.    fp = fopen(file_name, "wb");
  363.    if (!fp)
  364.       return;
  365.  
  366.    /* Create and initialize the png_struct with the desired error handler
  367.       functions.  If you want to use the default stderr and longjump method,
  368.       you can supply NULL for the last three parameters.  We also check that
  369.       the library version is compatible in case we are using dynamically
  370.       linked libraries.
  371.     */
  372.    png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
  373.       (void *)user_error_ptr, user_error_fn, user_warning_fn);
  374.  
  375.    if (!png_ptr)
  376.    {
  377.       fclose(fp);
  378.       return;
  379.    }
  380.  
  381.    info_ptr = png_create_info_struct(png_ptr);
  382.    if (!info_ptr)
  383.    {
  384.       fclose(fp);
  385.       png_destroy_write_struct(&png_ptr,  (png_infopp)NULL);
  386.       return;
  387.    }
  388.  
  389.    /* set error handling */
  390.    if (setjmp(png_ptr->jmpbuf))
  391.    {
  392.       /* If we get here, we had a problem reading the file */
  393.       fclose(fp);
  394.       png_destroy_write_struct(&png_ptr,  (png_infopp)NULL);
  395.       return;
  396.    }
  397.  
  398.    /* set up the output control if you are using standard C streams */
  399.    png_init_io(png_ptr, fp);
  400.  
  401.    /* if you are using replacement message functions, here you would call */
  402.    png_set_message_fn(png_ptr, (void *)msg_ptr, user_error_fn, user_warning_fn);
  403.    /* where msg_ptr is a structure you want available to the callbacks */
  404.  
  405.    /* set the file information here */
  406.    info_ptr->width = ;
  407.    info_ptr->height = ;
  408.    etc.
  409.  
  410.    /* set the palette if there is one */
  411.    info_ptr->valid |= PNG_INFO_PLTE;
  412.    info_ptr->palette = malloc(256 * sizeof (png_color));
  413.    info_ptr->num_palette = 256;
  414.    ... set palette colors ...
  415.  
  416.    /* optional significant bit chunk */
  417.    info_ptr->valid |= PNG_INFO_sBIT;
  418.    /* if we are dealing with a grayscale image then */
  419.    info_ptr->sig_bit.gray = true_bit_depth;
  420.    /* otherwise, if we are dealing with a color image then */
  421.    info_ptr->sig_bit.red = true_red_bit_depth;
  422.    info_ptr->sig_bit.green = true_green_bit_depth;
  423.    info_ptr->sig_bit.blue = true_blue_bit_depth;
  424.    /* if the image has an alpha channel then */
  425.    info_ptr->sig_bit.alpha = true_alpha_bit_depth;
  426.   
  427.    /* optional gamma chunk is strongly suggested if you have any guess
  428.       as to the correct gamma of the image */
  429.    info_ptr->valid |= PNG_INFO_gAMA;
  430.    info_ptr->gamma = gamma;
  431.  
  432.    /* other optional chunks like cHRM, bKGD, tRNS, tEXt, tIME, oFFs, pHYs, */
  433.  
  434.    /* write the file header information */
  435.    png_write_info(png_ptr, info_ptr);
  436.  
  437.    /* set up the transformations you want.  Note that these are
  438.       all optional.  Only call them if you want them */
  439.  
  440.    /* invert monocrome pixels */
  441.    png_set_invert(png_ptr);
  442.  
  443.    /* shift the pixels up to a legal bit depth and fill in
  444.       as appropriate to correctly scale the image */
  445.    png_set_shift(png_ptr, &(info_ptr->sig_bit));
  446.  
  447.    /* pack pixels into bytes */
  448.    png_set_packing(png_ptr);
  449.  
  450.    /* flip bgr pixels to rgb */
  451.    png_set_bgr(png_ptr);
  452.  
  453.    /* swap bytes of 16 bit files to most significant bit first */
  454.    png_set_swap(png_ptr);
  455.  
  456.    /* get rid of filler bytes, pack rgb into 3 bytes.  The
  457.       filler number is not used. */
  458.    png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
  459.  
  460.    /* turn on interlace handling if you are not using png_write_image() */
  461.    if (interlacing)
  462.       number_passes = png_set_interlace_handling(png_ptr);
  463.    else
  464.       number_passes = 1;
  465.  
  466.    /* the easiest way to write the image (you may choose to allocate the
  467.       memory differently, however) */
  468.    png_byte row_pointers[height][width];
  469.  
  470.    png_write_image(png_ptr, row_pointers);
  471.  
  472.    /* the other way to write the image - deal with interlacing */
  473.  
  474.    for (pass = 0; pass < number_passes; pass++)
  475.    {
  476.       /* Write a few rows at a time. */
  477.       png_write_rows(png_ptr, row_pointers, number_of_rows);
  478.  
  479.       /* If you are only writing one row at a time, this works */
  480.       for (y = 0; y < height; y++)
  481.       {
  482.          png_bytep row_pointers = row[y];
  483.          png_write_rows(png_ptr, &row_pointers, 1);
  484.       }
  485.    }
  486.  
  487.    /* You can write optional chunks like tEXt, tIME at the end as well.
  488.     * Note that if you wrote tEXt or zTXt chunks before the image, and
  489.     * you aren't writing out more at the end, you have to set
  490.     * info_ptr->num_text = 0 or they will be written out again.
  491.     */
  492.  
  493.    /* write the rest of the file */
  494.    png_write_end(png_ptr, info_ptr);
  495.  
  496.    /* if you malloced the palette, free it here */
  497.    if (info_ptr->palette)
  498.       free(info_ptr->palette);
  499.  
  500.    /* if you allocated any text comments, free them here */
  501.  
  502.    /* clean up after the write, and free any memory allocated */
  503.    png_destroy_write_struct(&png_ptr,  (png_infopp)NULL);
  504.  
  505.    /* close the file */
  506.    fclose(fp);
  507.  
  508.    /* that's it */
  509.    return;
  510. }
  511.  
  512.